import { Component,OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {HttpURLService} from "../../httpURL.service";

/**
 * 污水过程监控因子字典
 * guopeng
 * 2017.11.27
 */
export class WaterFactor {
  constructor(
    private id: string,//污染物id
    private waterFactorCode: string,//污水过程监控因子编码
    private waterFactorName: string,//污水过程监控因子名称
    private waterFactorUnit: string,//单位
    private waterFactorType: string,//类型
    private extensionInfo: string//拓展信息
  ){}
}

@Component({
  selector: 'app-waterFactor',
  templateUrl: './waterFactor.component.html',
  styleUrls: ['./waterFactor.component.css']
})
export class WaterFactorComponent implements OnInit{
  private waterFactorCode : string;//污水过程监控因子编码
  private waterFactorName : string;//污水过程监控因子名称
  errorMessage:string;//錯誤消息
  private list:Array<any>;

  public queryMessage:WaterFactor = new WaterFactor("","","","","","");
  public updateMessages:WaterFactor = new WaterFactor("","","","","","");
  public addMessages:WaterFactor =  new WaterFactor("","","","","","");
  public params; // 保存页面url参数
  public pageSize:any = 5;// 每页数据条数
  public curPage:any = 1;// 当前页码
  public totalNum: any = 0; // 总数据条数
  public totalPage: any = 0;// 总页数
  public url:string = this.httpURLService.getUrl("communication");

  constructor(private http: HttpClient,private httpURLService: HttpURLService){

  }
  add(){
    this.addMessages =  new WaterFactor("","","","","","");
  }

  userMessage(event:WaterFactor){
    let query = new WaterFactor("","","","","","");
    let update = new WaterFactor("","","","","","");
    Object.assign(query, event);
    Object.assign(update, event);
    this.queryMessage = query;
    this.updateMessages = update;
  }

  listMessage(info:any){
    let waterFactorCode = info.waterFactorCode== undefined ? '' : info.waterFactorCode ;
    let waterFactorName = info.waterFactorName== undefined ? '' : info.waterFactorName;
    this.http.get(this.url+'/waterFactor/queryWaterFactorListByPage?waterFactorCode='+waterFactorCode+'&waterFactorName='+waterFactorName).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

  addMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/waterFactor/addWaterFactor',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#add").modal('show');
          this.listMessage("");
          $('#addModal').modal('hide');
        }else if(data['resultCode']=='101119001'){
          this.errorMessage = "污水过程监控因子编码为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119002'){
          this.errorMessage = "污水过程监控因子名称为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119003'){
          this.errorMessage = "污水过程监控因子编码已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119004'){
          this.errorMessage = "污水过程监控因子名称已存在！";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }

  deleteById(event:any){
    let id = event.target.name;
    this.http.get(this.url+'/waterFactor/deleteWaterFactor/'+id).subscribe(data =>{
      if(data['resultCode']=='0'){
        $("#delete").modal('show');
        this.listMessage("");
        $('#deleteModal').modal('hide');
      }else if(data['resultCode']=='101119005'){
        this.errorMessage = "该污水过程监控因子不存在！";
        $("#error").modal('show');
      }
    })
  }

  updateMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/waterFactor/updateWaterFactor',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#update").modal('show');
          this.listMessage("");
          $('#updateModal').modal('hide');
        }else if(data['resultCode']=='101119001'){
          this.errorMessage = "污水过程监控因子编码为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119002'){
          this.errorMessage = "污水过程监控因子名称为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119003'){
          this.errorMessage = "污水过程监控因子编码已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119004'){
          this.errorMessage = "污水过程监控因子名称已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101119005'){
          this.errorMessage = "该污水过程监控因子不存在！";
          $("#error").modal('show');
        }
      })
    }else{
      $("#formError").modal('show');
    }
  }
  getPageData(pageNo) {
    let vm = this;
    vm.curPage = pageNo;
    let waterFactorCode = this.waterFactorCode== undefined ? '' : this.waterFactorCode ;
    let waterFactorName = this.waterFactorName== undefined ? '' : this.waterFactorName;
    let current = pageNo;
    this.http.get(this.url+'/waterFactor/queryWaterFactorListByPage?waterFactorCode='+waterFactorCode+'&waterFactorName='+waterFactorName+'&current='+current).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

	onstructor(){ }
	ngOnInit(){
    this.http.get(this.url+'/waterFactor/queryWaterFactorListByPage').subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
	}
}


